package com.pdstudio.carrecom.ui.activity.myclub;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.app.AppContext;
import com.pdstudio.carrecom.app.UIHelper;
import com.pdstudio.carrecom.bean.NewActivity;
import com.pdstudio.carrecom.bean.ResultInfo;
import com.pdstudio.carrecom.bean.UserProtocal;
import com.pdstudio.carrecom.ui.activity.more.ActivityUserProtocal;
import com.pdstudio.carrecom.ui.adapter.ImagePagerAdapter;
import com.pdstudio.carrecom.ui.fragment.FragmentHome.MyOnPageChangeListener;
import com.pdstudio.carrecom.ui.view.AutoScrollViewPager;
import com.pdstudio.carrecom.ui.view.CirclePageIndicator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 最新活动 类: ActivityNewActivity <br>
 * 描述: TODO <br>
 * 作者: xiangzhaoyu <br>
 * 时间: 2015-6-21 下午3:02:10
 */
public class ActivityNewActivity extends Activity {

	private View customLiveIndexTitleView;
	private TextView txtTitle;
	private ImageView ivBack;
	private Button mSignal;// 报名
	private TextView mTitle, mContent;

	private AutoScrollViewPager mViewPager;
	private CirclePageIndicator mCirclePageIndicator;
	private LinearLayout mLayoutIndictor;
	private List<NewActivity> nas;
	private int currentna;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myclub_newactivity);

		initTitle();
		InitialView();
		InitialData();
	}

	private void initTitle() {
		txtTitle = (TextView) findViewById(R.id.title_text_nav);

		txtTitle.setText("最新活动");

		ivBack = (ImageView) findViewById(R.id.title_back);
		ivBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void InitialView() {
		mTitle = (TextView) findViewById(R.id.tv_myclub_na_title);
		mContent = (TextView) findViewById(R.id.tv_myclub_an_introduce);

		mViewPager = (AutoScrollViewPager) findViewById(R.id.asv_myclub);

		mSignal = (Button) findViewById(R.id.btn_myclub_an_signalup);
		mSignal.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ActivityNewActivity.this,
						ActivityNewActivitySignal.class);
				intent.putExtra("act", nas.get(currentna));
				startActivity(intent);
			}
		});
	}

	private void InitialData() {

		try {
			HttpExecuteJson http = new HttpExecuteJson(this,
					mNewActivityListener);
			Map<String, Object> rps = new HashMap<String, Object>();
			rps.put("uid", AppContext.getInstance()._userId);
			rps.put("os", "android");
			rps.put("type", "10");
			rps.put("size", "30-60");
			http.get(ServiceHelper.NewActivity, rps);
		} catch (Exception e) {
			e.printStackTrace();
			mTitle.setText("无");
		}

	}

	private void DoJson(String json) {
		ResultInfo edv = null;
		try {
			edv = new Gson().fromJson(json, new TypeToken<ResultInfo>() {
			}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				// 原本在initview的代码放在这下面
				try {
					nas = new Gson().fromJson(new Gson().toJson(edv.data),
							new TypeToken<List<NewActivity>>() {
							}.getType());
					if (nas != null && nas.size() > 0) {
						if (nas.get(0) != null) {
							mTitle.setText(nas.get(0).title);
							mContent.setText(nas.get(0).content);
						}
						List<String> imageIdList = new ArrayList<String>();
						for (int i = 0; i < nas.size(); i++) {
							if (nas.get(i) != null
									&& (!nas.get(i).url.equals(null))
									&& (!nas.get(i).url.equals(""))) {
							}
							imageIdList.add(nas.get(i).url);

							mViewPager.setAdapter(new ImagePagerAdapter(this,
									imageIdList).setInfiniteLoop(true));
							mViewPager
									.setOnPageChangeListener(new MyOnPageChangeListener());

							mViewPager.setInterval(2000);
							mViewPager.startAutoScroll();

						}

					}
				} catch (Exception e) {
					e.printStackTrace();
					mTitle.setText("无");
				}
			} else {
				mTitle.setText("无");
			}
		} catch (Exception e) {
			e.printStackTrace();
			mTitle.setText("无");
		}

	}

	private httpReturnJson mNewActivityListener = new httpReturnJson() {
		@Override
		public void onFailure(int error, String msg) {
			UIHelper.ToastMessage(ActivityNewActivity.this, "获取最新活动失败" + msg);
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(ActivityNewActivity.this, "获取最新活动退出");
		}

		// 获取到饲料配方类型成功
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			// UIHelper.ToastMessage(_context,"总条数=" + result + "");
			DoJson(result);
		}
	};

	@Override
	public void onPause() {
		super.onPause();
		// stop auto scroll when onPause
		mViewPager.stopAutoScroll();
	}

	@Override
	public void onResume() {
		super.onResume();
		// start auto scroll when onResume
		mViewPager.startAutoScroll();
	}

	public class MyOnPageChangeListener implements
			ViewPager.OnPageChangeListener {

		@Override
		public void onPageSelected(int position) {
			try {
				int count=nas.size();
				currentna=position%count;
				
				mTitle.setText(nas.get(currentna).title);
				mContent.setText(nas.get(currentna).content);
			} catch (Exception e) {
			}
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
			try {
				int count=nas.size();
				currentna=position%count;
				
				mTitle.setText(nas.get(currentna).title);
				mContent.setText(nas.get(currentna).content);
			} catch (Exception e) {
			}
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	}

}
